﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuickStart.Properties;

namespace QuickStart.Forms
{
    public partial class ProgramAdding : Form
    {
        private MainForm mainForm;
        private bool isEditMode;  // false - add mode, true - edit mode
        private string originShortcut;

        public ProgramAdding()
        {
            InitializeComponent();

            this.Icon = Resources.QuickStartIcon;
        }

        public ProgramAdding(string shortcut, string path, MainForm form, bool isEditMode = false)
        {
            InitializeComponent();

            this.Icon = Resources.QuickStartIcon;

            this.txtShortcut.Text = shortcut;
            this.txtPath.Text = path;
            this.mainForm = form;
            this.isEditMode = isEditMode;
            this.originShortcut = shortcut;
        }


        private void btnSelect_Click(object sender, EventArgs e)
        {
            DialogResult ds = openFileDialog1.ShowDialog();
            if (ds == DialogResult.OK)
            {
                this.txtPath.Text = openFileDialog1.FileName;
                this.txtParameter.Text = "";
            }
        }

        
        private void btnOK_Click(object sender, EventArgs e)
        {
            string shortcut =this.txtShortcut.Text.Trim();
            string path = this.txtPath.Text.Trim();

            StartInfo si = StartInfo.GetInstance();

            if (!isEditMode) // add mode
            {
                if (!si.Validate(shortcut, path))
                {
                    MessageBox.Show("Shortcut or Path already exists!");
                    return;
                }
                si.Insert(shortcut, path);
              
            }
            else // edit mode
            {
                if (originShortcut != shortcut)
                {
                    if (!si.Validate(shortcut, path))
                    {
                        MessageBox.Show("Shortcut or Path already exists!");
                        return;
                    }
                }

                si.Modify(originShortcut, shortcut, path);
            }
            // MainForm.BindData
            this.mainForm.BindData();
          
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProgramAdding_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }


    }
}
